﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10._01.SortingAlgorithms
{
    class Program
    {
        private static int Partition(IList<int> collection, int left, int right)
        {
            var x = collection[right];
            int i = left - 1;
            int swapValue;

            for (int j = left; j < right; j++)
            {
                if (collection[j].CompareTo(x) < 0)
                {
                    i = i + 1;

                    swapValue = collection[i];
                    collection[i] = collection[j];
                    collection[j] = swapValue;
                }
            }

            swapValue = collection[i + 1];
            collection[i + 1] = collection[right];
            collection[right] = swapValue;

            return i + 1;
        }

        private static IList<int> QuickSort(IList<int> collection, int left, int right)
        {
            if (left < right)
            {
                int partition = Partition(collection, left, right);
                QuickSort(collection, left, partition - 1);
                QuickSort(collection, partition + 1, right);
            }

            return collection;
        }

        private static IList<int> SelectionSort(IList<int> collection)
        {
            int minValue = int.MinValue;

            for (int i = 0; i < collection.Count - 1; i++)
            {
                minValue = i;
                for (int j = i + 1; j < collection.Count; j++)
                {
                    if (collection[j].CompareTo(collection[minValue]) < 0)
                    {
                        minValue = j;
                    }
                }

                if (minValue != i)
                {
                    int swapValue = collection[i];
                    collection[i] = collection[minValue];
                    collection[minValue] = swapValue;
                }
            }

            return collection;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number ");
            int N = int.Parse(Console.ReadLine());

            IList<int> collection = GenerateRandomNumbers(N);

            if (collection.Count >= 10)
            {
                collection = QuickSort(collection, 0, collection.Count - 1);
            }
            else
            {
                collection = SelectionSort(collection);
            }

            Console.WriteLine(string.Join(" ", collection));
        }

        private static IList<int> GenerateRandomNumbers(int N)
        {
            Random random = new Random();
            var collection = new List<int>();

            for (int i = 0; i < N; i++)
            {
                collection.Add(random.Next(1, 1000));
            }

            return collection;
        }
    }
}
